home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 6 / CU Amiga Magazine's Super CD-ROM 06 (1996)(EMAP Images)(GB)(Track 1 of 4)[!][issue 1997-01].iso / cucd / prog / mui / muirexx2.2 / demos / muidir / comm.rexx < prev    next >
OS/2 REXX Batch file  |  1996-08-06  |  16KB  |  579 lines

  1. /*
  2.  
  3. Code:       comm.rexx
  4. Author:     Russell Leighton
  5. Revision:   11 Jan 1996
  6.  
  7. Comments:   This is script used to perform file operations.  It is executed
  8. when certain gadgets are selected in the muidir GUI.
  9.  
  10. */
  11.  
  12. options results
  13. signal on error
  14.  
  15. /* TAG ID definitions */
  16.  
  17. Draggable =                       0x80420b6e /* V11 isg BOOL              */
  18. Dirlist_Directory =               0x8042ea41 /* V4  isg STRPTR            */
  19. Dirlist_RejectIcons =             0x80424808 /* V4  is. BOOL              */
  20. List_Quiet =                      0x8042d8c7 /* V4  .s. BOOL              */
  21.  
  22. /* TAG variable definitions */
  23.  
  24. TRUE = 1
  25. FALSE = 0
  26. List_Insert_Bottom = -3
  27.  
  28. parse arg portname comm '['name']'
  29.  
  30. call pragma('Directory','muidir:')
  31.  
  32. address VALUE portname
  33.  
  34. comm = strip(comm)
  35.  
  36. group ID DIR REGISTER
  37. ndir = result
  38.  
  39. select
  40.  
  41. /* copy the selected files from the source to destination directory */
  42.  
  43.     when index(comm,'COPY') = 1 then call copyfiles(3)
  44.  
  45. /* move the selected files from the source to destination directory */
  46.  
  47.     when index(comm,'MOVE') = 1 then do
  48.         dirlist ID DIR||ndir ATTRS Dirlist_Directory
  49.         sdir = import(d2c(result))
  50.         dirlist ID DIR||(3-ndir) ATTRS Dirlist_Directory
  51.         ddir = import(d2c(result))
  52.         if substr(sdir,1,pos(':',sdir)) = substr(ddir,1,pos(':',ddir)) then
  53.             copy = 1
  54.         else copy = 2
  55.         call copyfiles(copy)
  56.     end
  57.  
  58. /* delete the selected files from the source directory */
  59.  
  60.     when index(comm,'DELETE') = 1 then do
  61.         request ID MDIR TITLE '" "' GADGETS '"OK|Cancel"' '"Delete selected entries?"'
  62.         if result = 1 then do
  63.             do forever
  64.                 dirlist ID DIR||ndir
  65.                 sfile = result
  66.                 if sfile = '' then break
  67.                 address command 'delete > T:err "'sfile'" ALL QUIET'
  68.                 list ID HST INSERT POS List_Insert_Bottom STRING 'delete 'sfile' ALL QUIET'
  69.                 check ID ICN||ndir
  70.                 if result = '1' & exists(sfile'.info') then do
  71.                     address command 'delete > T:err "'sfile'.info" ALL QUIET'
  72.                     list ID HST INSERT POS List_Insert_Bottom STRING 'delete 'sfile'.info ALL QUIET'
  73.                 end
  74.             end
  75.             if exists('T:err') then address command 'delete T:err quiet'
  76.             check ID ICN||ndir
  77.             dirlist ID DIR||ndir REREAD ATTRS Dirlist_RejectIcons result
  78.         end
  79.     end
  80.  
  81. /* either display the selected files icon or change source to the selected 
  82.    directory */
  83.  
  84.     when index(comm,'DIR') = 1 then do
  85.         name = strip(name)
  86.         if index(statef(name),'DIR') ~= 0 then do
  87.  
  88. /* the selected entry is a directory, so change to it */
  89.  
  90.             call changedir(name)
  91.             exit
  92.         end
  93.         else do
  94.  
  95. /* otherwise, if the entry does not exist assume it is suppose to be a
  96. directory and create it */
  97.  
  98.             if ~exists(name) then do
  99.                 address command 'makedir "'name'"'
  100.                 list ID HST INSERT POS List_Insert_Bottom STRING 'makedir 'name
  101.                 if rc = 0 then call changedir(name)
  102.                 exit
  103.             end
  104.             call defcomm portname name
  105.         end
  106.     end
  107.     when index(comm,'ICON') = 1 then do
  108.         name = strip(name)
  109.         if lastpos('/',name) ~= 0 then file = substr(name,lastpos('/',name)+1)
  110.         else file = substr(name,lastpos(':',name)+1)
  111.  
  112. /*      text ID NAM||ndir LABEL file */
  113.  
  114.         group ID GRP||ndir
  115.             button ID IMG||ndir ICON deficon(name) COMMAND '"muidir:defcomm 'portname' %s"' ATTRS Draggable TRUE LABEL name
  116.         endgroup
  117.     end
  118.  
  119. /* clear the text icon and string */
  120.  
  121.     when index(comm,'CLEAR') = 1 then do
  122.         group ID GRP||ndir
  123.             image ID IMG||ndir ICON '""' LABEL ' '
  124.         endgroup
  125.     end
  126.     when index(comm,'HCLEAR') = 1 then do
  127.         list ID HST STRING
  128.     end
  129.     when index(comm,'HEXE') = 1 then do
  130.         do forever
  131.  
  132.         /* get next entry */
  133.  
  134.             list ID HST
  135.             line = result
  136.             if line = '' then break
  137.  
  138.         /* execute entry */
  139.  
  140.             address command line' >T:err'
  141.         end
  142.         if exists('T:err') then address command 'delete T:err quiet'
  143.     end
  144.  
  145. /* compile a list of files to copy from the source to destination directory */
  146.  
  147.     when index(comm,'CCOPY') = 1 then do
  148.  
  149.         /* clear the copy list */
  150.  
  151.         list ID CLST STRING
  152.  
  153.         /* get the source directory name */
  154.  
  155.         dirlist ID DIR||ndir ATTRS Dirlist_Directory
  156.         sname = import(d2c(result))
  157.  
  158.         /* get the destination directory name */
  159.  
  160.         dirlist ID DIR||(3-ndir) ATTRS Dirlist_Directory
  161.         dname = import(d2c(result))
  162.  
  163.         /* temporarily disable list update */
  164.  
  165.         list ID CLST ATTRS List_Quiet TRUE
  166.  
  167.         /* call the nextcopy procedure to process the current directory */
  168.  
  169.         call nextcopy sname';'dname
  170.  
  171.         /* update the copy list (i.e. display the contents) */
  172.  
  173.         list ID CLST ATTRS List_Quiet FALSE
  174.     end
  175.  
  176. /* perform copy based on list of compiled files */
  177.  
  178.     when index(comm,'MCOPY') = 1 then do
  179.         do forever
  180.  
  181.         /* get next entry */
  182.  
  183.             list ID CLST
  184.             line = result
  185.             if line = '' then break
  186.  
  187.         /* parse entry */
  188.  
  189.             parse var line sfile' -> 'dfile' ['flags']'
  190.  
  191.         /* execute copy */
  192.  
  193.             address command 'copy > T:err "'sfile'" to "'dfile'" 'flags' CLONE QUIET'
  194.         end
  195.         if exists('T:err') then address command 'delete T:err quiet'
  196.  
  197.         /* update dirlist */
  198.  
  199.         check ID ICN||(3-ndir)
  200.         dirlist ID DIR||(3-ndir) REREAD ATTRS Dirlist_RejectIcons result
  201.     end
  202.  
  203. /* compile a list of files to delete from the destination directory */
  204.  
  205.     when index(comm,'CDEL') = 1 then do
  206.  
  207.         /* clear the delete list */
  208.  
  209.         list ID DLST STRING
  210.  
  211.         /* get the source directory name */
  212.  
  213.         dirlist ID DIR||ndir ATTRS Dirlist_Directory
  214.         sname = import(d2c(result))
  215.  
  216.         /* get the destination directory name */
  217.  
  218.         dirlist ID DIR||(3-ndir) ATTRS Dirlist_Directory
  219.         dname = import(d2c(result))
  220.  
  221.         /* temporarily disable list update */
  222.  
  223.         list ID DLST ATTRS List_Quiet TRUE
  224.  
  225.         /* call the nextdel procedure to process the current directory */
  226.  
  227.         call nextdel sname';'dname
  228.  
  229.         /* update the delete list (i.e. display the contents) */
  230.  
  231.         list ID DLST ATTRS List_Quiet FALSE
  232.     end
  233.  
  234. /* perform delete based on list of compiled files */
  235.  
  236.     when index(comm,'MDEL') = 1 then do
  237.         do forever
  238.  
  239.         /* get next entry */
  240.  
  241.             list ID DLST
  242.             line = result
  243.             if line = '' then break
  244.  
  245.         /* parse entry */
  246.  
  247.             parse var line sfile' ['flags']'
  248.  
  249.         /* execute delete */
  250.  
  251.             address command 'delete > T:err "'sfile'" 'flags' QUIET'
  252.         end
  253.         if exists('T:err') then address command 'delete T:err quiet'
  254.  
  255.         /* update dirlist */
  256.  
  257.         check ID ICN||(3-ndir)
  258.         dirlist ID DIR||(3-ndir) REREAD ATTRS Dirlist_RejectIcons result
  259.     end
  260.  
  261. /* Edit selected files */
  262.  
  263.     when index(comm,'EDIT') = 1 then do
  264.         getvar screen
  265.         pub = result
  266.         files = ''
  267.         do forever
  268.             dirlist ID DIR||ndir
  269.             sfile = result
  270.             if sfile = '' then break
  271.             files = files' "'sfile'"'
  272.         end
  273.         address command 'ced -pubscreen='pub' 'files
  274.     end
  275.  
  276. /* View selected files */
  277.  
  278.     when index(comm,'VIEW') = 1 then do
  279.         files = ''
  280.         do forever
  281.             dirlist ID DIR||ndir
  282.             sfile = result
  283.             if sfile = '' then break
  284.             files = files' "'sfile'"'
  285.         end
  286.         address command 'tools:graphics/PicassoII/Viewer/IntuiView now'files
  287.     end
  288.  
  289. /* Make icons for selected files */
  290.  
  291.     when index(comm,'ICON') = 1 then do
  292.         files = ''
  293.         do forever
  294.             dirlist ID DIR||ndir
  295.             sfile = result
  296.             if sfile = '' then break
  297.             files = files' "'sfile'"'
  298.         end
  299.         address command 'tools:graphics/Picticon/Picticon ADDICON=YES CHUNKYMODE=YES FREE_ICON_POS=YES NEWICON=YES OVERWRITE=YES QUIET=YES TEMPLATE_ICON=tools:graphics/Picticon/envsys/def_picture'files
  300.     end
  301.  
  302. /* Execute selected files */
  303.  
  304.     when index(comm,'EXECUTE') = 1 then do
  305.         do forever
  306.             dirlist ID DIR||ndir
  307.             sfile = result
  308.             if sfile = '' then break
  309.             address command sfile
  310.         end
  311.     end
  312. end
  313.  
  314. exit
  315.  
  316. /* procedure to change the source dirlist directory */
  317.  
  318. changedir:
  319. parse arg name
  320.  
  321.     dirlist ID DIR||ndir ATTRS Dirlist_Directory
  322.     if result = 0 then dirname = ''
  323.     else dirname = import(d2c(result))
  324.  
  325. /* if the name is empty then set directory to RAM: */
  326.  
  327.     if strip(name) = '' then name = 'RAM:'
  328.  
  329. /* if the name is the currently displayed directory then just reread it */
  330.  
  331.     if name = dirname then do
  332.         check ID ICN||ndir
  333.         dirlist ID DIR||ndir REREAD ATTRS Dirlist_RejectIcons result
  334.         return
  335.     end
  336.  
  337. /* if the name is a / then get parent directory */
  338.  
  339.     if index(name,'/') = 1 then do
  340.         if lastpos('/',dirname) ~= 0 then name = substr(dirname,1,lastpos('/',dirname)-1)
  341.         else name = substr(dirname,1,lastpos(':',dirname))
  342.     end
  343.  
  344. /* if the name is not a directory then extract directory name */
  345.  
  346.     if index(statef(name),'DIR') = 0 then do
  347.         if lastpos('/',name) ~= 0 then name = substr(name,1,lastpos('/',name)-1)
  348.         else name = substr(name,1,lastpos(':',name))
  349.     end
  350.  
  351. /* determine volume name and percentage full */
  352.  
  353.     vol = substr(name,1,pos(':',name))
  354.     address command 'assign >pipe: exists 'vol
  355.     call open('pipe','pipe:','R')
  356.     line = readln('pipe')
  357.     call close('pipe')
  358.     parse var line vname aname
  359.     aname = strip(aname)
  360.     if index(aname,'[Mounted]') = 1 then vname = vname':'
  361.     else do
  362.         if aname = '' then vname = vol
  363.         else vname = substr(aname,1,lastpos(':',aname))
  364.     end
  365.     address command 'info >pipe: 'vname
  366.     call open('pipe','pipe:','R')
  367.     line = readln('pipe')
  368.     line = readln('pipe')
  369.     line = readln('pipe')
  370.     line = readln('pipe')
  371.     parse var line unit size used free full errs 42 status 53 vname
  372.     do until eof('pipe')
  373.         line = readln('pipe')
  374.     end
  375.     call close('pipe')
  376.  
  377.     if lastpos('/',name) = length(name) then name = substr(name,1,length(name)-1)
  378.  
  379. /* set appropriate gadgets with information */
  380.  
  381.     text ID TXT||ndir LABEL vname'  'full' full'
  382.     string ID SRC||ndir CONTENT name
  383.     check ID ICN||ndir
  384.     dirlist ID DIR||ndir PATH '"'name'"' ATTRS Dirlist_RejectIcons result
  385.     group ID REG REGISTER LABEL '"Directory"'
  386.     list ID LST NODUP INSERT STRING name
  387. return
  388.  
  389. /* procedure to copy, rename, or move files from source to destination */
  390.  
  391. copyfiles:
  392. parse arg type
  393.  
  394. dirlist ID DIR||(3-ndir) ATTRS Dirlist_Directory
  395. ddir = import(d2c(result))
  396. if lastpos('/',ddir) ~= length(ddir) then do
  397.     if lastpos(':',ddir) ~= length(ddir) then ddir = ddir'/'
  398. end
  399.  
  400. do forever
  401.  
  402. /* find next selected file */
  403.  
  404.     dirlist ID DIR||ndir
  405.     sfile = result
  406.  
  407. /* if there is no selected file then reread dirlist and return */
  408.  
  409.     if sfile = '' then do
  410.         check ID ICN||ndir
  411.         flag = result
  412.         dirlist ID DIR||ndir REREAD ATTRS Dirlist_RejectIcons flag
  413.         dirlist ID DIR||(3-ndir) REREAD ATTRS Dirlist_RejectIcons flag
  414.         return
  415.     end
  416.     if lastpos('/',sfile) ~= 0 then dfile = ddir||substr(sfile,lastpos('/',sfile)+1)
  417.     else dfile = ddir||substr(sfile,lastpos(':',sfile)+1)
  418.  
  419. /* if the file exists at the destination then put up a requester to allow
  420. the user to decide on the action */
  421.  
  422.     if exists(dfile) & (type > 0) then do
  423.         request ID MDIR TITLE '" "' GADGETS '"_OK|_ALL|_SKIP|_CANCEL"' 'bFile already exists!n Do you wish to overwrite?'
  424.         select
  425.             when result = 0 then return
  426.             when result = 1 then nop
  427.             when result = 2 then type = 0-type
  428.             when result = 3 then iterate
  429.             otherwise return
  430.         end
  431.     end
  432.  
  433.     if type < 0 then ctype = 0 - type
  434.     else ctype = type
  435.  
  436.     call file portname ndir ctype sfile';'dfile
  437. end
  438. return
  439.  
  440. /* this procedure is used to determine names of files and directories to be
  441.    copied.  It is called recursivily to traverse directory trees. */
  442.  
  443. nextcopy: procedure
  444. parse arg sname';'dname
  445.  
  446. sname = strip(sname)
  447. dname = strip(dname)
  448. if sname = '' | dname = '' then return
  449.  
  450. /* get list of entries in source directory */
  451.  
  452. slist = showdir(sname,'ALL',';')
  453.  
  454. if lastpos('/',sname) ~= length(sname) then do
  455.     if lastpos(':',sname) ~= length(sname) then sname = sname'/'
  456. end
  457. if lastpos('/',dname) ~= length(dname) then do
  458.     if lastpos(':',dname) ~= length(dname) then dname = dname'/'
  459. end
  460.  
  461. /* process each entry */
  462.  
  463. do while slist ~= ''
  464.     parse var slist sfile';'slist
  465.  
  466. /* assemble full path names for source and destination files */
  467.  
  468.     dfile = dname||sfile
  469.     sfile = sname||sfile
  470.  
  471. /* get file information */
  472.  
  473.     state = statef(sfile)
  474.     parse var state type size blocks protect s1 s2 s3 .
  475.  
  476. /* create time numerical stamp */
  477.  
  478.     sstamp = right(s1,4,'0')||right(s2,4,'0')||right(s3,4,'0')
  479.  
  480. /* if destination file exists then do the following */
  481.  
  482.     if exists(dfile) then do
  483.  
  484. /* if entry is a directory then recursivily call this routine with the
  485.    subdirectory name as the argument */
  486.  
  487.         if index(type,'DIR') then call nextcopy sfile';'dfile
  488.  
  489. /* else compare the destination time stamp to the source */
  490.  
  491.         else do
  492.             state = statef(dfile)
  493.             parse var state type size blocks protect s1 s2 s3 .
  494.             dstamp = right(s1,4,'0')||right(s2,4,'0')||right(s3,4,'0')
  495.  
  496. /* if the source file is newer then add entry to copy list */
  497.  
  498.             if sstamp > dstamp then do
  499.                 list ID CLST INSERT POS List_Insert_Bottom STRING sfile '->' dfile' [] *'
  500.             end
  501.         end
  502.     end
  503.  
  504. /* if destination file does not exist then add entry to copy list */
  505.  
  506.     else do
  507.         if index(type,'DIR') then list ID CLST INSERT POS List_Insert_Bottom STRING sfile '->' dfile' [ALL]'
  508.         else list ID CLST INSERT POS List_Insert_Bottom STRING sfile '->' dfile' []'
  509.     end
  510. end
  511. return
  512.  
  513. /* this procedure is used to determine names of files and directories to be
  514.    deleted.  It is called recursivily to traverse directory trees. */
  515.  
  516. nextdel: procedure
  517. parse arg sname';'dname
  518.  
  519. sname = strip(sname)
  520. dname = strip(dname)
  521. if sname = '' | dname = '' then return
  522.  
  523. /* get list of entries in destination directory */
  524.  
  525. dlist = showdir(dname,'ALL',';')
  526.  
  527. if lastpos('/',sname) ~= length(sname) then do
  528.     if lastpos(':',sname) ~= length(sname) then sname = sname'/'
  529. end
  530. if lastpos('/',dname) ~= length(dname) then do
  531.     if lastpos(':',dname) ~= length(dname) then dname = dname'/'
  532. end
  533.  
  534. /* process each entry */
  535.  
  536. do while dlist ~= ''
  537.     parse var dlist dfile';'dlist
  538.  
  539. /* assemble full path names for source and destination files */
  540.  
  541.     sfile = sname||dfile
  542.     dfile = dname||dfile
  543.  
  544. /* get file information */
  545.  
  546.     state = statef(dfile)
  547.     parse var state type size blocks protect stamp
  548.  
  549. /* if source file exists then do the following */
  550.  
  551.     if exists(sfile) then do
  552.  
  553. /* if entry is a directory then recursivily call this routine with the
  554.  
  555.    subdirectory name as the argument */
  556.         if index(type,'DIR') then call nextdel sfile';'dfile
  557.     end
  558.  
  559. /* else add the file or directory to the delete list */
  560.  
  561.     else do
  562.         if index(type,'DIR') then list ID DLST INSERT POS List_Insert_Bottom STRING dfile' [ALL]'
  563.         else list ID DLST INSERT POS List_Insert_Bottom STRING dfile' []'
  564.     end
  565. end
  566. return
  567.  
  568. /* if an error occurs then display it */
  569.  
  570. error:
  571.     if exists('T:err') then do
  572.         call open('err','T:err')
  573.         err = readln('err')
  574.         call close('err')
  575.         address command 'delete T:err quiet'
  576.         request ID MDIR TITLE '" "' GADGETS '"OK"' '"'err'"'
  577.     end
  578. exit
  579.